package com.zhong.oss.adapter;

import com.zhong.oss.entity.FileInfo;
import com.zhong.oss.utils.MinioUntil;
import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * minio文件存储
 */
public class MinioStorageAdapter implements StorageAdapter {

    @Resource
    private MinioUntil minioUntil;

    /**
     * 创建bucket
     */
    @Override
    @SneakyThrows
    public void createBucket(String bucket) {
        minioUntil.createBucket(bucket);
    }

    /**
     * 上传文件
     */

    @SneakyThrows
    public void uploadFile(MultipartFile uploadFile, String bucket, String objectName) {
        minioUntil.createBucket(bucket);
        if (objectName != null) {
            minioUntil.uploadFile(uploadFile.getInputStream(), bucket, objectName + "/" + uploadFile.getName());
        } else {
            minioUntil.uploadFile(uploadFile.getInputStream(), bucket, uploadFile.getName());
        }
    }

    /**
     * 列出所有桶
     */
    @Override
    @SneakyThrows
    public List<String> getBucketAll() {
        return minioUntil.getBucketAll();
    }

    /**
     * 列出指定桶内的所有文件
     */
    @Override
    @SneakyThrows
    public List<FileInfo> getFileAll(String bucket) {
        return minioUntil.getFileAll(bucket);
    }

    /**
     * 下载文件
     */
    @Override
    @SneakyThrows
    public InputStream downLoad(String bucket, String objectName) {
        return minioUntil.downLoad(bucket, objectName);
    }

    /**
     * 删除桶
     */
    @Override
    @SneakyThrows
    public void deleteBucket(String bucket) {
        minioUntil.deleteBucket(bucket);
    }

    /**
     * 删除文件
     */
    @Override
    @SneakyThrows
    public void deleteObject(String bucket, String objectName) {
        minioUntil.deleteObject(bucket, objectName);
    }
}
